package com.haisen.controller;


import com.alibaba.fastjson.JSON;
import com.haisen.core.annotation.SystemLog;
import com.haisen.core.utils.PageResultSet;
import com.haisen.core.utils.Result;
import com.haisen.dataobject.dto.ItemParamGroupDto;
import com.haisen.dataobject.entity.ItemCat;
import com.haisen.dataobject.entity.ItemParamGroup;
import com.haisen.dataobject.query.ItemParamGroupQuery;
import com.haisen.service.ItemCatService;
import com.haisen.service.ItemParamGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/itemparamgroup")
@Slf4j
public class ItemParamGroupController {

    @Autowired
    private ItemCatService itemCatService;

    @Autowired
    private ItemParamGroupService itemParamGroupService;

    @GetMapping
    @RequiresPermissions("itemparamgroup:view")
    public String page(Model model){
        /**
         * 获取商品目录
         */
        setCommonData(model);
        return "product/itemparamgroup";
    }


    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("itemparamgroup:view")
    public PageResultSet<ItemParamGroupDto> list(ItemParamGroupQuery itemParamGroupQuery) {
        log.info("查询条件：{}", JSON.toJSONString(itemParamGroupQuery));
        return itemParamGroupService.findByPage(itemParamGroupQuery);
    }



    /**
     * 根据产品种类的属性iD，查所有的子外键
     * @param catId
     * @return
     */
   /* @ResponseBody
    @RequestMapping("/listkey")
    @RequiresPermissions("itemparamgroup:view")
    public Result list(@RequestParam("cid") Long catId) {
        ItemCat itemCat = itemCatService.findOneById(catId);
        return Result.success(itemParamGroupService.findGroups(itemCat.getParmGroupIdList()));
    }*/

    @ResponseBody
    @PostMapping("/create")
    @RequiresPermissions("itemparamgroup:create")
    @SystemLog("商品管理添加参数组")
    public Result create(@Valid ItemParamGroup itemParamGroup){
        itemParamGroupService.create(itemParamGroup);
        return Result.success();
    }

    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("itemparamgroup:update")
    @SystemLog("商品管理修改参数组")
    public Result update(@Valid  ItemParamGroup itemParamGroup){
        itemParamGroupService.update(itemParamGroup);
        return Result.success();
    }

    @ResponseBody
    @PostMapping("/delete")
    @RequiresPermissions("itemparamgroup:delete")
    @SystemLog("商品管理删除删除组")
    public Result delete(@RequestParam("id") Long[] ids){

        //是否将long转化为integer
//       Integer[] intids = (Integer[]) Arrays.stream(ids).map(e -> new Integer(e.intValue())).toArray();
        itemParamGroupService.deleteBatch(ids);
        return Result.success();
    }


    /**
     * 设置公共属性，是因为这个页面上每个方法都要
     * @param model
     */
    private void setCommonData(Model model) {
        List<ItemCat> itemCatList = itemCatService.findAll();
        //log.info("规格：itemCatList{}", JSON.toJSONString(itemCatList));
        model.addAttribute("itemCatList", itemCatList);

    }


}
